package uk.ac.glasgow.demosocs.users;
import java.io.*;

public class UserTextReader {

	private String filename = "users.txt";
	
	public Electorate readFromText(){
		Electorate electorate = new Electorate("electorate");
		try{
	        System.out.println(System.getProperty("user.dir"));

	        FileInputStream fstream = new FileInputStream(filename);

	        DataInputStream in = new DataInputStream(fstream);
	        BufferedReader br = new BufferedReader(new InputStreamReader(in));
	        String strLine;
	        //Read File Line By Line
	        int counter = 0;
	        while ((strLine = br.readLine()) != null)   {
	            String[] line = strLine.split("-");
	            if(counter>0){
	            	//check admin
	            	if(line[4].equals(Voter.ADMIN)){
	            		Admin admin = new Admin(line[1],line[2],line[0],line[3]);
	            		//add the admin to collection
	            		electorate.addAnExistingVoter(admin);
	            		
	            	} else{
	            		//create a general voter
	            		Voter voter = new Voter(line[1],line[2],line[0],line[3]);
	            		electorate.addAnExistingVoter(voter);
	            	}
	            	
	            		
	            }//end if counter
	            counter++;
	        }
		} catch(Exception e){
            System.err.println("Error: " + e.getMessage());

		}
		return electorate;
	}
}
